package com.edou.auth.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.RowBounds;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.edou.auth.dao.IRoleDao;
import com.edou.auth.dao.IRoleRightDao;
import com.edou.auth.dao.IUserDao;
import com.edou.auth.dao.IUserRoleDao;
import com.edou.auth.dto.RoleCondition;
import com.edou.auth.entity.Role;
import com.edou.auth.entity.RoleRight;
import com.edou.auth.entity.User;
import com.edou.auth.service.IRoleService;
import com.edou.base.constant.SystemConstant;
import com.edou.base.page.PageResult;

/**
 * 角色service实现类
 * @creator     admin
 * @create-time 2016年2月15日   上午11:48:11
 * @email admin@edou.com
 * @company www.edou.com
 * @version 1.0
 */
@Service("roleServiceImpl")
@Transactional("transactionManager")
public class RoleServiceImpl implements IRoleService{
	
	@Resource(name = "roleDaoImpl")
	private IRoleDao roleDao;
	@Resource(name = "userDaoImpl")
	private IUserDao userDao;
	@Resource(name = "userRoleDaoImpl")
	private IUserRoleDao userRoleDao;
	@Resource(name = "roleRightDaoImpl")
	private IRoleRightDao roleRightDao;
	
	
	/**
	 * 查询所有角色
	 * @return
	 */
	public List<Role> listRoleAll(){
		return roleDao.listRoleAll();
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	@Override
	public void listRoleByPage(PageResult pageResult,RoleCondition condition) {
		//1.查询总数
		int count = roleDao.listRoleByPageCount(condition);
		pageResult.setRows(count);
		
		//2.查询区间数据
		RowBounds bounds = new RowBounds(pageResult.getCurrentPageIndex(), pageResult.getPageSize());
		List<Role> roleList = roleDao.listRoleByPage(bounds,condition);
		
		for(Role curRole : roleList){
			StringBuffer users = new StringBuffer();
			List<User> userList = userRoleDao.listUserByRoleId(curRole.getId(),SystemConstant.SEARCH_ROLE_USER_COUNT);
			for(int j=0; j < userList.size();j++){
				if(j > 0){
					users.append(",");
				}
				User user = userList.get(j);
				users.append(user.getName());
			}
			curRole.setUserNames(users.toString());
		}
		
		pageResult.setResult(roleList);
	}
	
	@Override
	public Role getRoleById(String id) {
		return roleDao.getRoleById(id);
	}

	@Override
	public void saveRole(Role item) {
		if(StringUtils.isBlank(item.getId())){
			//新增
			roleDao.add(item);
		}else{
			Role oldItem = roleDao.getRoleById(item.getId());
			oldItem.setName(item.getName());
			oldItem.setCode(item.getCode());
			oldItem.setRemark(item.getRemark());
			//修改
			roleDao.update(oldItem);
			//清空角色权限
			roleRightDao.delByRoleId(item.getId());
		}
		
		//保存角色权限
		String[] rightIds = item.getRightIds().split(",");
		for(String rightId: rightIds){
			RoleRight roleRight = new RoleRight();
			roleRight.setRightId(rightId);
			roleRight.setRoleId(item.getId());
			roleRightDao.add(roleRight);
		}
		
	}

	@Override
	public void delByIds(String[] ids) {
		for(String id : ids){
			//1.删除角色权限
			roleRightDao.delByRoleId(id);
			//2.删除角色
			roleDao.delByIds(new String[]{id});
		}
		
	}

	@Override
	public boolean checkCodeRepeat(Role item) {
		int count = roleDao.getCountByCode(item);
		return count > 0;
	}

	/**
	 * 设置角色类型
	 * @param ids
	 * @param type
	 */
	@Override
	public void setSysRole(String[] ids, Integer type) {
		for(String id : ids){
			Role item = roleDao.getRoleById(id);
			item.setType(type);
			roleDao.update(item);
		}
	}

	/**
	 * 根据用户id查询角色列表
	 * @param userId
	 * @return
	 */
	public List<Role> listRoleByUserId(String userId) {
		return roleDao.listRoleByUserId(userId);
	}

	/**
	 * 根据角色代码查询角色
	 * @param code
	 * @return
	 */
	public Role getRoleByCode(String code) {
		return roleDao.getRoleByCode(code);
	}

}
